package com.atguigu.flink.chapter11;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;


public class Flink06_SQL_BaseUse {
       public static void main(String[] args) throws Exception {
               Configuration configuration = new Configuration();
               //web  UI端口
               configuration.setInteger("rest.prot",10000);
           StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(configuration);
               env.setParallelism(1);

           DataStreamSource<WaterSensor> waterSensorStream =
                   env.fromElements(
                           new WaterSensor("sensor_1", 1000L, 10),
                           new WaterSensor("sensor_1", 2000L, 20),
                           new WaterSensor("sensor_2", 3000L, 30),
                           new WaterSensor("sensor_1", 4000L, 40),
                           new WaterSensor("sensor_1", 5000L, 50),
                           new WaterSensor("sensor_2", 6000L, 60));

           // 1. 创建表的执行环境
           StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
           // 2. 创建表: 将流转换成动态表. 表的字段名从pojo的属性名自动抽取  ,表的列名默认就是属性名
           Table table = tableEnv.fromDataStream(waterSensorStream);

          // tableEnv.executeSql("sal语句");// 执行ddl ，或者 dml 中的增删改 ，没有查
         //  tableEnv.sqlQuery("sql语句"); // 执行查询语句

           //1.查询未注册的表
          // Table result = tableEnv.sqlQuery("select * from " + table + " where id = 'sensor_1'");
           // from  where 之间的 " 需要增加一个空格  ！！！ 不然要报错  --》》  from " + table + " where

          //    result.execute().print();

           // 2 查询已经注册的表
           // 2.1 给table注册一个临时表
           tableEnv.createTemporaryView("sensor", table);
           tableEnv.sqlQuery("select * from sensor where id='sensor_1'").execute().print();







       }
}
